package com.etrans.web.gps.business.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.base.domain.VehicleExpand;
import com.etrans.web.gps.business.base.domain.VehicleInfo;
import com.etrans.web.gps.util.ParseXml;
import com.etrans.web.gps.util.WebServiceUtil;


@Service
public class VehicleManagerservice extends BaseService{
	
	public List<Map<String, Object>> queryVehicleList(Map<String, Object> paramMap) {
		List<Map<String, Object>> list = selectList("vehicleManagerQuery.queryVehicleList", paramMap);
		return list;
	}

	public Integer queryVehicleListCount(Map<String, Object> paramMap) {
		return (Integer) this.selectOne("vehicleManagerQuery.queryVehicleListCount", paramMap);
	}

/*	public List queryAllRegistrationNOColors() {
		return selectList("vehicleManagerQuery.queryAllRegistrationNOColors","");
	}*/

	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void saveVehicle(Map<String, Object> params) {
		
		this.insert("vehicleManagerMapper.saveVehicle", params);

	}

	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void updateVehicle(Map<String, Object> params) {
		this.update("vehicleManagerMapper.updateVehicle", params);
		
	}

	/**
	 * 根据ID获取车辆信息
	 * 
	 * @param id
	 * @return
	 */
	public VehicleExpand queryVehicleById(Integer id) {
		VehicleExpand vehicleExpand = (VehicleExpand) this.selectOne("vehicleManagerQuery.queryVehicleById", id);
		return vehicleExpand;
	}
	
	public VehicleInfo queryVehicleInfoById(Integer id) {
		
		return this.selectOne("vehicleManagerQuery.queryVehicleInfoById", id);
	}
	
	public Map<String,Object> queryVehicleVoById(Integer id) {
		
		return this.selectOne("vehicleManagerQuery.queryVehicleVoById", id);
	}
	@Transactional(readOnly = false, rollbackFor = {DataAccessException.class,Exception.class})
	public void deleteVehicleByID(HashMap<String, Object> param) {
		// 删除车辆信息
		this.update("vehicleManagerMapper.deleteVehicleByID", param);
	}
	@Transactional(readOnly = false, rollbackFor = {DataAccessException.class,Exception.class})
	public void cancelVehicleByID(HashMap<String, Object> param) {
		// 注销车辆信息，虚拟删除
		this.update("vehicleManagerMapper.cancelVehicleByID", param);
	}
/*	
	public List<TerminalKind> queryTerminalKinds() {
		return selectList("vehicleManagerQuery.queryTerminalKinds", 1);
	}

	public List<VehicleKind> queryVehicleKinds() {
		return selectList("vehicleManagerQuery.queryVehicleKinds", 1);
	}

	public List<HashMap<String, Object>> queryVehicleByRegistrationnoAndColor(HashMap<String, Object> param) {
		return selectList("vehicleManagerQuery.queryVehicleByRegistrationnoAndColor", param);
	}

	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void saveImportVehicle(VehicleExpand vehicle,SimCardVO sim,Terminal terminal){
		simCardService.saveSimCard(sim);
		terminal.setSimId(sim.getId());
		terminalService.saveTerminal(terminal);
		vehicle.setTerminalId(terminal.getId());
		this.insert("vehicleManagerMapper.saveVehicle", vehicle);
		//启用
		vehicle.setUseFlag(1);
		this.update("vehicleManagerMapper.updateTerminalStatusSQL", vehicle);
	}


	*//**
	 * 更新车辆使用状态
	 * 
	 * @param vehicle
	 *//*
	@Transactional(readOnly = false, rollbackFor = {DataAccessException.class,Exception.class})
	public void updateVehicleWorkStatus(Integer VehicleID, HttpServletRequest request) {
		Vehicle vehicemodel = this.queryVehicleById(VehicleID);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 0表示当前状态为使用，需要停用
		if (vehicemodel.getWorkStatus() != null) {
			if (vehicemodel.getWorkStatus().equals(0)) {
				vehicemodel.setWorkStatus(1);
			}else {// 启用
				vehicemodel.setWorkStatus(0);
			}
		}else{
			vehicemodel.setWorkStatus(0);
		}
		vehicemodel.setModifyDateTime(dateFormat.format(new Date()));
		this.update("vehicleManagerMapper.updateVehicleWorkStatusSQL", vehicemodel);
		
	}

	// 选择停用的终端进行更换
	@Transactional(readOnly = false, rollbackFor = {DataAccessException.class, Exception.class})
	public void changeTerminal_sel( VehicleExpand vehicle) {
		// 先更改车辆终端信息
		this.update("vehicleManagerMapper.UpdateTerminal", vehicle);
		// 激活新终端
		Integer oldTerminalId = vehicle.getOldTerminalId();
		Integer terminalId = vehicle.getTerminalId();
		// 原终端销户
		if (oldTerminalId != null) {
			vehicle.setUseFlag(0);
			vehicle.setTerminalId(oldTerminalId);
			this.update("vehicleManagerMapper.updateTerminalStatusSQL", vehicle);
		}
		// 新终端开户
		if (terminalId != null) {
			vehicle.setUseFlag(1);
			vehicle.setTerminalId(terminalId);
			this.update("vehicleManagerMapper.updateTerminalStatusSQL", vehicle);
		}
	}

	// 输入新终端号进行更换
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void changeTerminal_input(HttpServletRequest request,VehicleExpand vehicle) {
		//修改前的终端ID
		Integer oldTerminalId = vehicle.getOldTerminalId();
		//获取页面输入的新通信号，终端类型，创建新的终端通信好
		String newCommno = vehicle.getNewCommNo();
		Integer terminalkindid = vehicle.getTerminalKindId();
		Terminal oldTerminal = null;
		Integer simId = null;
		// 原终端销户(原终端与车辆解绑)
		if (oldTerminalId != null) {
			vehicle.setUseFlag(0);
			vehicle.setTerminalId(oldTerminalId);
			this.update("vehicleManagerMapper.updateTerminalStatusSQL", vehicle);
			oldTerminal = terminalService.queryTerminalById(oldTerminalId);
			//旧终端解除与sim卡关系
			if(oldTerminal != null){
				simId = oldTerminal.getSimId();
				oldTerminal.setSimId(null);
				terminalService.updateTerminalInfo(oldTerminal);
			}
		}
		Terminal newTerminal = oldTerminal;
		if(newTerminal == null)
			newTerminal = new Terminal();
		newTerminal.setCommNo(newCommno);
		if(simId !=null ){
			newTerminal.setSimId(simId);
		}
		if(terminalkindid != null){
			newTerminal.setTerminalKindId(terminalkindid);
		}
		*//*******由于创建一个新的终端信息,所以需要绑定企业信息********//*
		if(vehicle.getWorkUnitId()!= null){
			newTerminal.setWorkUnitId(vehicle.getWorkUnitId());
		}
		terminalService.saveTerminal(newTerminal);
		vehicle.setUseFlag(1);
		vehicle.setTerminalId(newTerminal.getId());
		this.update("vehicleManagerMapper.updateTerminalStatusSQL", vehicle);
		//绑定新的终端ID
		this.update("vehicleManagerMapper.UpdateTerminal", vehicle);
	}
	*//**
	 * 根据车辆查看终端参数
	 * 
	 * @param param
	 * @return
	 *//*
	public List queryTerminalParamByVehicleID(Map<String, Object> paramMap) {
		List list = selectList("vehicleManagerQuery.queryTerminalParamByVehicleID", paramMap);
		return list;
	}
	
	*//**
	 * 根据车辆查看终端参数(获取条数)
	 * 
	 * @param param
	 * @return
	 *//*
	public Integer queryCountTerminalParamByVehicleID(Map<String, Object> param) {
		return (Integer) this.selectOne("vehicleManagerQuery.queryCountTerminalParamByVehicleID", param);
	}

	*//**
	 * 根据车辆ID查找多边形信息
	 * @param VehicleID
	 * @return
	 *//*
	public List<HashMap<String, Object>> queryPolygonInfoByVehicleID(Map<String, Object> paramMap) {
		List list = selectList("vehicleManagerQuery.queryPolygonInfoByVehicleID", paramMap);
		return list;
	}
	
	*//**
	 * 根据车辆ID查找多边形信息(条数)
	 * @param param
	 * @return
	 *//*
	public Integer queryCountPolygonInfoByVehicleID(Map<String, Object> param) {
		return (Integer) this.selectOne("vehicleManagerQuery.queryCountPolygonInfoByVehicleID", param);
	}
	*//**
	 * 根据车辆ID查找圆形信息
	 * @param VehicleID
	 * @return
	 *//*
	public List queryRoundInfoByVehicleID(Map<String, Object> paramMap) {
		List list = selectList("vehicleManagerQuery.queryRoundInfoByVehicleID", paramMap);
		return list;
	}
	*//**
	 * 根据车辆ID查找圆形信息(条数)
	 * @param param
	 * @return
	 *//*
	public Integer queryCountRoundInfoByVehicleID(Map<String, Object> paramMap) {
		return (Integer) this.selectOne("vehicleManagerQuery.queryCountRoundInfoByVehicleID", paramMap);
	}
	*//**
	 * 根据车辆ID查找矩形信息
	 * @param VehicleID
	 * @return
	 *//*
	public List queryRectangleInfoByVehicleID(Map<String, Object> paramMap) {
		List list = selectList("vehicleManagerQuery.queryRectangleInfoByVehicleID", paramMap);
		return list;
	}
	*//**
	 * 根据车辆ID查找矩形信息(条数)
	 * @param param
	 * @return
	 *//*
	public Integer queryCountRectangleInfoByVehicleID(Map<String, Object> paramMap) {
		return (Integer) this.selectOne("vehicleManagerQuery.queryCountRectangleInfoByVehicleID", paramMap);
	}

	public List queryOilSignDetails(Map<String, Object> paramMap) {
		List list = selectList("vehicleManagerQuery.queryOilSignDetails", paramMap);
		return list;
	}

	public Integer OilSignDetailsCount(Map<String, Object> paramMap) {
		return (Integer) this.selectOne("vehicleManagerQuery.OilSignDetailsCount", paramMap);
	}

	public List queryAllOilconfigure() {
		List list = selectList("vehicleManagerQuery.queryAllOilconfigure",new HashMap());
		return list;
	}*/

	public int checkIfExistNumber(Map<String, Object> params) {
		return (Integer)this.selectOne("vehicleManagerQuery.checkIfExistNumber", params);
	}

	/*public List<HashMap<String, Object>> queryLevelVehicleTree( Map<String, Object> paramMap) {
		return this.selectList( "vehicleManagerQuery.queryLevelVehicleTree", paramMap);
	}

	public int queryCountByRegistrationNO(HashMap<String, Object> parms) {
		// TODO Auto-generated method stub
		return 0;
	}*/

	public int checkIfExistPermitsion(Map<String, Object> params) {
		return (Integer)this.selectOne("vehicleManagerQuery.checkIfExistPermitsion", params);
	}

	public VehicleInfo queryVehicleDetail(Integer vehicleId) {
		return this.selectOne("vehicleManagerQuery.queryVehicleDetail", vehicleId);
	}
	/**
	 * 从运政平台取数据同步车辆信息
	 * @param cphm
	 * @param cpys
	 * @param yszhm
	 * @param ziparea
	 */
	public boolean synVeicleInfo(String cphm, String cpys, String yszhm,String ziparea) {
		boolean flag = true;
		Map<String, Object> param = new HashMap<>();
		try{
			param =WebServiceUtil.getCarInfoXml(cphm,cpys,yszhm,ziparea);
//			String xml="<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetCarInfoResponse xmlns='http://tempuri.org/'><GetCarInfoResult><xs:schema id='GetCarInfo' xmlns='' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'><xs:element name='GetCarInfo' msdata:IsDataSet='true' msdata:MainDataTable='Vehicle' msdata:UseCurrentLocale='true'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='Vehicle'><xs:complexType><xs:sequence><xs:element name='OperatingID' type='xs:string' minOccurs='0' /><xs:element name='OwnerUnit' type='xs:string' minOccurs='0' /><xs:element name='CarNumber' type='xs:string' minOccurs='0' /><xs:element name='FactoryID' type='xs:string' minOccurs='0' /><xs:element name='OperatingType' type='xs:string' minOccurs='0' /><xs:element name='CarType' type='xs:string' minOccurs='0' /><xs:element name='NumberColor' type='xs:string' minOccurs='0' /><xs:element name='Capacity' type='xs:string' minOccurs='0' /><xs:element name='VclLenght' type='xs:string' minOccurs='0' /><xs:element name='VclWidth' type='xs:string' minOccurs='0' /><xs:element name='VclHeight' type='xs:string' minOccurs='0' /><xs:element name='VclState' type='xs:string' minOccurs='0' /><xs:element name='VclTecLevel' type='xs:string' minOccurs='0' /><xs:element name='CurCheckDate' type='xs:string' minOccurs='0' /><xs:element name='NextCheckDate' type='xs:string' minOccurs='0' /><xs:element name='EngineNumber' type='xs:string' minOccurs='0' /><xs:element name='VIN' type='xs:string' minOccurs='0' /><xs:element name='REMARK' type='xs:string' minOccurs='0' /><xs:element name='PeccancyState' type='xs:string' minOccurs='0' /><xs:element name='LicenseNumber' type='xs:string' minOccurs='0' /><xs:element name='LicenseDate' type='xs:string' minOccurs='0' /><xs:element name='OUTMILLDATE' type='xs:string' minOccurs='0' /><xs:element name='FuelType' type='xs:string' minOccurs='0' /><xs:element name='NextAssessDate' type='xs:string' minOccurs='0' /><xs:element name='MType' type='xs:string' minOccurs='0' /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'><GetCarInfo xmlns=''><Vehicle diffgr:id='Vehicle1' msdata:rowOrder='0' diffgr:hasChanges='inserted'><OperatingID>450100030183</OperatingID><OwnerUnit>南宁大明山运输有限责任公司</OwnerUnit><CarNumber>贵H17751</CarNumber><FactoryID>柯斯达牌SCT6705GRB53LEXY</FactoryID><OperatingType>县内包车客运,县际包车客运</OperatingType><CarType>大型普通客车</CarType><NumberColor>黄色</NumberColor><Capacity>20座</Capacity><VclLenght>7005</VclLenght><VclWidth>2040</VclWidth><VclHeight>2772</VclHeight><VclState>营运</VclState><VclTecLevel>一级</VclTecLevel><CurCheckDate>2018-03-28</CurCheckDate><NextCheckDate>2019-03-19</NextCheckDate><EngineNumber>H186633</EngineNumber><VIN>LFME55813JS011367</VIN><REMARK>GPS0</REMARK><PeccancyState>正常</PeccancyState><LicenseNumber>450103101651</LicenseNumber><LicenseDate>2018-03-28</LicenseDate><OUTMILLDATE>2018-01-03</OUTMILLDATE><FuelType>汽油</FuelType><NextAssessDate>2018-09-26</NextAssessDate><MType>柯斯达牌SCT6705GRB53LEXY</MType></Vehicle></GetCarInfo></diffgr:diffgram></GetCarInfoResult><ErrMsg /></GetCarInfoResponse></soap:Body></soap:Envelope>";
//			ParseXml parse = new ParseXml();
//			param = parse.parse(xml);
			if(param.get("CarNumber")!=null&&param.get("NumberColor")!=null){
				List<Map<String,Object>> list = this.selectList("vehicleManagerQuery.queryVehicleColorId",param);
				if(list!=null&&list.size()>0){
					param.put("colorId", list.get(0).get("id"));
				}

				this.update("vehicleManagerMapper.synVehicleInfo", param);
			}else{
				flag =false;
			}
			return flag;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 车辆颜色集
	 * @return
	 */
	public List<Map<String,Object>> getVehicleColor(){
		List<Map<String,Object>> list = this.selectList("vehicleManagerQuery.queryVehicleColor",null);
		return list;
	}
	/**
	 * 车辆行业集
	 * @return
	 */
	public List<Map<String,Object>> getVehicleTrades(){
		List<Map<String,Object>> list = this.selectList("vehicleManagerQuery.queryVehicleTrade",null);
		return list;
	}
	/**
	 * 车辆业务集
	 * @return
	 */
	public List<Map<String,Object>> getVehicleOwner(){
		List<Map<String,Object>> list = this.selectList("vehicleManagerQuery.queryVehicleOwner",null);
		return list;
	}
	
	/**
	 * 车辆类型集
	 * @return
	 */
	public List<Map<String,Object>> getVehicleCarType(){
		List<Map<String,Object>> list = this.selectList("vehicleManagerQuery.queryVehicleCarType",null);
		return list;
	}
	/**
	 * 保存导入车辆信息
	 * @param params
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class,Exception.class })
	public void saveVehicleImport(Map<String, Object> params) {
		this.insert("vehicleManagerMapper.saveVehicleImport", params);
	}
	/**
	 * 导入是否存在营运证号
	 * @param params
	 * @return
	 */
	public int existPermitsion(Map<String, Object> params) {
		return (Integer)this.selectOne("vehicleManagerQuery.existPermitsion", params);
	}
}
